﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace API_Service
{
    public static class SCUDictionary
    {
        private static Dictionary<string, UserDentity> dentityDict = new Dictionary<string, UserDentity>();

        private static System.Timers.Timer CheckTimer = new System.Timers.Timer(60 * 1000);

        static SCUDictionary()
        {
            CheckTimer.Enabled = true;
            CheckTimer.Elapsed += CheckTimer_Elapsed;
        }

        private static void CheckTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            //foreach (var user in TokenList)
            //{
            //    if ((DateTime.Now - user.Value.TimeStamp).TotalMinutes >= 60)
            //    {
            //        TokenList.Remove(user.Key);
            //    }
            //}
        }

        public static ExecutionResult AddItem(string token, UserDentity userData)
        {
            ExecutionResult model = new ExecutionResult();

            if (dentityDict.ContainsKey(token))
            {
                UserDentity userInfo = dentityDict[token];
            }
            else
            {
                dentityDict.Add(token, userData);
            }
            
            model.Status = true;

            return model;
        }

        public static Dictionary<string, UserDentity> TokenList
        {
            get
            {
                return dentityDict;
            }
        }

        public static UserDentity GetUserData(string token)
        {
            UserDentity userData = TokenList[token];

            if (userData == null)
            {
                return new UserDentity() { UserID = "Debugger", UserName = "Debugger",  };
            }
            else
            {
                return userData;
            }
        }

        public static bool Exist(string key)
        {
            return dentityDict.ContainsKey(key);
        }
    }
}